
package com.task.base;


import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.LinkedList;

public class CommonAdapter extends BaseAdapter {

    private LinkedList<Item> items = null;

    private Resources res = null;
    
    private LayoutInflater mInflater = null;
    
    private View.OnClickListener listener=null;

    public CommonAdapter(Context context, LinkedList<Item> items,View.OnClickListener listener) {
        mInflater = LayoutInflater.from(context);
        res = context.getResources();
        this.listener=listener;
        this.items = items;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items == null ? 0 : items.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            vh = new ViewHolder();
            vh.icon = (ImageView) convertView.findViewById(R.id.itemImage);
            vh.name = (TextView) convertView.findViewById(R.id.name);
            vh.info_mem = (TextView) convertView.findViewById(R.id.infoMem);
            vh.info_cpu = (TextView) convertView.findViewById(R.id.infoCpu);
            vh.imagebutton = (ImageButton) convertView.findViewById(R.id.buttonClose);
            convertView.setTag(vh);

        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        Item item = items.get(position);
        vh.icon.setImageDrawable(res.getDrawable(item.getIconId()));
        vh.name.setText(item.getName());
        vh.info_mem.setText(item.getInfoMem());
        vh.info_cpu.setText(item.getInfoCpu());
        //vh.button.setText(item.getButtonName());
        vh.imagebutton.setTag(item.getName());
        vh.imagebutton.setOnClickListener(listener);
        vh.imagebutton.setFocusable(false);//由于按钮会和listView抢夺焦点所有需要设置一下

        return convertView;
    }


}
